﻿using AccumulateApp.Extention;
using AccumulateApp.Helper;
using AccumulateApp.Model;
using Newtonsoft.Json;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Media;

namespace AccumulateApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
        }

        /// <summary>
        /// 多语言切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedLanguage = cmbLanguage.SelectedItem as ComboBoxItem;
            if (selectedLanguage != null)
            {
                var tag = selectedLanguage.Tag.ToString();
                ResourceDictionary rd = new ResourceDictionary();
                rd.Source = new Uri($"Language/{tag}.xaml", UriKind.Relative);

                var index = System.Windows.Application.Current.Resources.MergedDictionaries.Count;
                System.Windows.Application.Current.Resources.MergedDictionaries[index - 1] = rd;
            }
        }

        /// <summary>
        /// 包含图片的类序列化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageSerializeButton_Click(object sender, RoutedEventArgs e)
        {
            CustomeTool customeTool = new CustomeTool()
            {
                Name = "Tool1",
                InputValue = 1.0,
                OutputValue = 2.0
            };

        }

        /// <summary>
        /// 包含图片的类反序列化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageDeserializeButton_Click(object sender, RoutedEventArgs e)
        {
            var serializeProperty = CommonHelper.ReadJsonFile<SerializeProperty>("SerializeProperty.json");
            if (serializeProperty != null)
            {
                var toolObject = serializeProperty.ToolData;
                CustomeTool customeTool = JsonConvert.DeserializeObject<CustomeTool>(serializeProperty.ToolData);

            }
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void ShowRightPanelButton_Click(object sender, RoutedEventArgs e)
        {
            // 确保 RightPanel 可见
            PanleIn(RightPanel);
        }

        private void ButtonRightPanelClose_Click(object sender, RoutedEventArgs e)
        {
            PanleOut(RightPanel);
        }

        private void PanleIn(Grid grid)
        {
            PopPanel.Visibility = Visibility.Visible;
            grid.Visibility = Visibility.Visible;
            var transform = grid.RenderTransform as TranslateTransform;
            if (transform == null)
            {
                transform = new TranslateTransform() { X = grid.Width };
                grid.RenderTransform = transform;
            }
            var animation = new DoubleAnimation
            {
                From = grid.Width,
                To = 0,
                Duration = TimeSpan.FromMilliseconds(200),
                EasingFunction = new QuadraticEase()
            };
            transform.BeginAnimation(TranslateTransform.XProperty, animation);
        }

        private void PanleOut(Grid grid)
        {
            var transform = grid.RenderTransform as TranslateTransform;
            var animation = new DoubleAnimation
            {
                From = 0,
                To = grid.Width,
                Duration = TimeSpan.FromMilliseconds(200),
                EasingFunction = new QuadraticEase()
            };
            animation.Completed += (s, a) =>
            {
                // 动画完成后隐藏 RightPanel
                PopPanel.Visibility = Visibility.Collapsed;
                grid.Visibility = Visibility.Collapsed;
            };
            transform.BeginAnimation(TranslateTransform.XProperty, animation);
        }
    }
}